<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/2/19
 * Time: 20:20
 */

namespace common;


use app\models\SiteConfig;
use Yii;

class Utils
{
    public static function getParams($key,$defaultValue=null)
    {
        $request = Yii::$app->request;
        $get = $request->get($key);
        $post = $request->getBodyParam($key);
        if($get!=null)
            return $get;
        else if($post!=null)
            return $post;
        else
            return $defaultValue;
    }


    public static function getTranslate($object,$prefix)
    {
        return $object->attributes[$prefix.'_'.substr(Yii::$app->language,0,2)];
    }

    public static function getKeyWords()
    {
        $cacheKey = 'keywords';
        $cache =  Yii::$app->cache;
        $data = $cache->get($cacheKey);
        if ($data === false) {
            // $data 已过期，或者在缓存中找不到
            $keys = SiteConfig::findOne(['name'=>'keywords']);
            if($keys)
                $data = $keys->content;
            $cache->set($cacheKey, $data, 3600);
        }
        return $data;
    }
}